package util;

import model.Book;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * @Question BookDao
 * @Date 2022/12/17 20:19
 * @Solution
 */
public class BookDao {
    public List<Book> getAll() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Book> bookList=new LinkedList<>();
        try {
            connection=DBUtil.getConnection();
            String sql = "select * from book";
            statement=connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while(resultSet.next()) {
                Book book = new Book();
                book.setBookId(resultSet.getInt("book_id"));
                book.setBookAuthor(resultSet.getString("book_author"));
                book.setBookName(resultSet.getString("book_name"));
                book.setBookPre(resultSet.getString("book_press"));
                book.setState(resultSet.getBoolean("state"));
                bookList.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return bookList;
    }
    public int deleteById(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int ret=0;
        try {
            connection=DBUtil.getConnection();
            String sql = "delete from book where book_id=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,id);
            ret = statement.executeUpdate();
            return ret;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return ret;
    }
    public int reviseId(int oldId,int newId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int ret=0;
        try {
            connection=DBUtil.getConnection();
            String sql = "update book set book_id=? where book_id=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,newId);
            statement.setInt(2,oldId);
            ret = statement.executeUpdate();
            return ret;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return ret;
    }
    public int reviseName(int id,String newName) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int ret=0;
        try {
            connection=DBUtil.getConnection();
            String sql = "update book set book_name=? where book_id=?";
            statement=connection.prepareStatement(sql);
            statement.setString(1,newName);
            statement.setInt(2,id);
            ret = statement.executeUpdate();
            return ret;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return ret;
    }
    public int reviseAuthor(int id,String newAuthor) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int ret=0;
        try {
            connection=DBUtil.getConnection();
            String sql = "update book set book_author=? where book_id=?";
            statement=connection.prepareStatement(sql);
            statement.setString(1,newAuthor);
            statement.setInt(2,id);
            ret = statement.executeUpdate();
            return ret;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return ret;
    }
    public int revisePress(int id,String newPress) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int ret=0;
        try {
            connection=DBUtil.getConnection();
            String sql = "update book set book_press=? where book_id=?";
            statement=connection.prepareStatement(sql);
            statement.setString(1,newPress);
            statement.setInt(2,id);
            ret = statement.executeUpdate();
            return ret;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return ret;
    }
    public int reviseState(int id,int newState) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int ret=0;
        try {
            connection=DBUtil.getConnection();
            String sql = "update book set state=? where book_id=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,newState);
            statement.setInt(2,id);
            ret = statement.executeUpdate();
            return ret;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return ret;
    }
    public int insert(String bookId,String bookName,String bookAuthor,String bookPress) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int ret=0;
        try {
            connection=DBUtil.getConnection();
            String sql = "insert into book values(?,?,?,?,1)";
            statement=connection.prepareStatement(sql);
            statement.setString(1,bookId);
            statement.setString(2,bookName);
            statement.setString(3,bookAuthor);
            statement.setString(4,bookPress);
            ret = statement.executeUpdate();
            return ret;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return ret;
    }
}
